package logic

import (
	"context"
	"errors"
	"renting-demo/model/mysql"
	"strconv"
	"time"

	"renting-demo/houseorder_srv/houseorder"
	"renting-demo/houseorder_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderListLogic {
	return &OrderListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderListLogic) OrderList(in *houseorder.OrderListRequest) (*houseorder.OrderListResponse, error) {
	// todo: add your logic here and delete this line
	search, res := mysql.HouseOrderList(in.Page, in.Search)
	if len(res) == 0 {
		return nil, errors.New("服务器异常")
	}
	var product []*houseorder.OrderHouse
	for _, v := range res {
		product = append(product, &houseorder.OrderHouse{
			ID:          int64(v.ID),
			OrderNumber: v.OrderNumber,
			UserID:      strconv.FormatInt(v.UserId, 10),
			HouseId:     strconv.FormatInt(v.HouseId, 10),
			BeginDate:   v.BeginDate.Format(time.DateTime),
			EndDate:     v.EndDate.Format(time.DateTime),
			HousePrice:  v.HousePrice,
			Amount:      v.Amount,
			Status:      v.Status,
		})
	}

	return &houseorder.OrderListResponse{
		Orders: product,
		Search: search,
	}, nil
}
